using HKB.Common;
using HKB.Data;
using System;
using System.Data;
namespace HKB.Web
{
	public class Pagination
	{
		public string SQL_LeftJoinTable;
		public string SQL_LeftJoinField;
		public string SQL_Fields;
		public string SQL_JoinField;
		public string SQL_JoinField2;
		public bool SQL_CloseTablePre = true;
		public int P_indexPage = 1;
		public int P_MaxPerPage;
		public int P_allNum = -1;
		public string P_Object;
		public string P_LinkHead;
		public bool P_isShowDownList = true;
		private string getallnumsql;
		public string allNumSql
		{
			get
			{
				return this.getallnumsql;
			}
		}
		public string GetSQL(string m_Table, int m_Page, int m_MaxPerPage, string TableKey, string m_Where, string m_OrderBy)
		{
			string text = "";
			if (this.SQL_CloseTablePre)
			{
				text = "[" + m_Table + "].";
			}
			string text2 = "";
			string text3 = "";
			string text4 = text + "*";
			this.P_MaxPerPage = m_MaxPerPage;
			if (this.P_MaxPerPage <= 0)
			{
				this.P_MaxPerPage = 10;
			}
			if (this.SQL_LeftJoinTable != null)
			{
				text2 = string.Concat(new string[]
				{
					" Left Join [",
					this.SQL_LeftJoinTable,
					"] On [",
					this.SQL_LeftJoinTable,
					"].[",
					this.SQL_JoinField,
					"] = [",
					m_Table,
					"].[",
					this.SQL_JoinField2,
					"] "
				});
				string[] array = this.SQL_LeftJoinField.Split(new char[]
				{
					','
				});
				string[] array2 = array;
				for (int i = 0; i < array2.Length; i++)
				{
					string text5 = array2[i];
					text3 = string.Concat(new string[]
					{
						text3,
						", [",
						this.SQL_LeftJoinTable,
						"].",
						text5
					});
				}
			}
			this.getallnumsql = string.Concat(new string[]
			{
				"Select count(",
				m_Table,
				".",
				TableKey,
				") From [",
				m_Table,
				"] ",
				text2,
				" ",
				m_Where
			});
			if (this.SQL_Fields != string.Empty && this.SQL_Fields != null)
			{
				text4 = "";
				string[] array3 = this.SQL_Fields.Split(new char[]
				{
					','
				});
				string[] array2 = array3;
				for (int i = 0; i < array2.Length; i++)
				{
					string text5 = array2[i];
					text4 = string.Concat(new string[]
					{
						text4,
						", ",
						text,
						text5,
						" "
					});
				}
				if (text4 != "")
				{
					text4 = "," + text4;
					text4 = text4.Replace(",,", "");
				}
			}
			if (string.IsNullOrEmpty(m_OrderBy))
			{
				m_OrderBy = TableKey + " Desc";
			}
			if (m_Page <= 0)
			{
				m_Page = 1;
			}
			this.P_indexPage = m_Page;
			return string.Concat(new string[]
			{
				"Select ",
				text4,
				text3,
				" From [",
				m_Table,
				"] ",
				text2,
				" ",
				m_Where,
				" Order By ",
				text,
				m_OrderBy
			});
		}
		public DataTable GetDataTable(string m_Table, int m_Page, int m_MaxPerPage, string TableKey, string m_Where, string m_OrderBy, Conn conn, IDbDataParameter[] param)
		{
			string sQL = this.GetSQL(m_Table, m_Page, m_MaxPerPage, TableKey, m_Where, m_OrderBy);
			DataTable result;
			if (param != null)
			{
				if (this.P_allNum == -1)
				{
					this.P_allNum = conn.ExecuteCount(this.allNumSql, param);
				}
				SQLDataReader sQLDataReader = new SQLDataReader();
				DataTable dataTable = sQLDataReader.RangeQuery(sQL, (long)((m_Page - 1) * m_MaxPerPage), (long)(m_Page * m_MaxPerPage - 1), param).Tables[0];
				sQLDataReader.Close();
				result = dataTable;
			}
			else
			{
				if (this.P_allNum == -1)
				{
					this.P_allNum = conn.ExecuteCount(this.allNumSql);
				}
				SQLDataReader sQLDataReader = new SQLDataReader();
				DataTable dataTable = sQLDataReader.RangeQuery(sQL, (long)((m_Page - 1) * m_MaxPerPage), (long)(m_Page * m_MaxPerPage - 1), null).Tables[0];
				sQLDataReader.Close();
				result = dataTable;
			}
			return result;
		}
		public string ShowPagePanel()
		{
			int num = (int)Math.Ceiling(Convert.ToDouble(this.P_allNum) / Convert.ToDouble(this.P_MaxPerPage));
			if (num == 0)
			{
				num = 1;
			}
			if (this.P_indexPage > num)
			{
				this.P_indexPage = num;
			}
			string text = "";
			if (this.P_LinkHead != null && this.P_LinkHead != "")
			{
				if (this.P_LinkHead.Substring(0, 1) != "&")
				{
					this.P_LinkHead = "&" + this.P_LinkHead;
				}
			}
			string text2 = text;
			text = string.Concat(new string[]
			{
				text2,
				Language.GetMemberMsg("all", new string[0]),
				" ",
				this.P_allNum.ToString(),
				" ",
				this.P_Object,
				" "
			});
			object obj;
			if (this.P_indexPage == 1)
			{
				text = text + Language.GetMemberMsg("PagedFirst", new string[0]) + "   ";
				text = text + Language.GetMemberMsg("PagedOn", new string[0]) + "   ";
			}
			else
			{
				obj = text;
				text = string.Concat(new object[]
				{
					obj,
					"<a href='?Page=1&maxperpage=",
					this.P_MaxPerPage,
					this.P_LinkHead,
					"'>",
					Language.GetMemberMsg("PagedFirst", new string[0]),
					"</a>   "
				});
				obj = text;
				text = string.Concat(new object[]
				{
					obj,
					"<a href='?Page=",
					this.P_indexPage - 1,
					"&maxperpage=",
					this.P_MaxPerPage,
					this.P_LinkHead,
					"'>",
					Language.GetMemberMsg("PagedOn", new string[0]),
					"</a>   "
				});
			}
			int num2;
			if (this.P_indexPage == num)
			{
				num2 = num;
			}
			else
			{
				num2 = this.P_indexPage + 1;
			}
			if (this.P_indexPage == num)
			{
				text = text + Language.GetMemberMsg("PagedDown", new string[0]) + "   ";
				text = text + Language.GetMemberMsg("PagedLast", new string[0]) + "   ";
			}
			else
			{
				obj = text;
				text = string.Concat(new object[]
				{
					obj,
					"<a href='?Page=",
					num2,
					"&maxperpage=",
					this.P_MaxPerPage,
					this.P_LinkHead,
					"'>",
					Language.GetMemberMsg("PagedDown", new string[0]),
					"</a>    "
				});
				obj = text;
				text = string.Concat(new object[]
				{
					obj,
					"<a href='?Page=",
					num.ToString(),
					"&maxperpage=",
					this.P_MaxPerPage,
					this.P_LinkHead,
					"'>",
					Language.GetMemberMsg("PagedLast", new string[0]),
					"</a>   "
				});
			}
			obj = text;
			text = string.Concat(new object[]
			{
				obj,
				Language.GetMemberMmbsg("Paged2"),
				":",
				this.P_indexPage,
				"/",
				num,
				Language.GetMemberMsg("Paged", new string[0]),
				" "
			});
			obj = text;
			text = string.Concat(new object[]
			{
				obj,
				" ",
				this.P_MaxPerPage,
				" ",
				this.P_Object,
				"/",
				Language.GetMemberMsg("Paged", new string[0])
			});
			if (this.P_isShowDownList)
			{
				obj = text;
				text = string.Concat(new object[]
				{
					obj,
					Language.GetMemberMsg("To", new string[0]),
					":<select name='page' onChange=\"javascript:location.href='?Page='+this.options[this.selectedIndex].value+'&maxperpage=",
					this.P_MaxPerPage,
					this.P_LinkHead,
					"';\">"
				});
				for (int i = 1; i <= num; i++)
				{
					if (i >= 1000)
					{
						break;
					}
					obj = text;
					text = string.Concat(new object[]
					{
						obj,
						"<option value='",
						i,
						"'"
					});
					if (i == this.P_indexPage)
					{
						text += " selected ";
					}
					obj = text;
					text = string.Concat(new object[]
					{
						obj,
						">",
						Language.GetMemberMsg("Article", new string[0]),
						i,
						Language.GetMemberMsg("Paged", new string[0]),
						"</option>"
					});
				}
				text += "</select>";
			}
			return text;
		}
		public string ShowPagePanelJs()
		{
			int num = (int)Math.Ceiling(Convert.ToDouble(this.P_allNum) / Convert.ToDouble(this.P_MaxPerPage));
			if (num == 0)
			{
				num = 1;
			}
			if (this.P_indexPage > num)
			{
				this.P_indexPage = num;
			}
			string text = "";
			string text2 = text;
			text = string.Concat(new string[]
			{
				text2,
				Language.GetMemberMsg("all", new string[0]),
				this.P_allNum.ToString(),
				this.P_Object,
				"  "
			});
			object obj;
			if (this.P_indexPage == 1)
			{
				text = text + Language.GetMemberMsg("PagedFirst", new string[0]) + "  ";
				text = text + Language.GetMemberMsg("PagedOn", new string[0]) + "  ";
			}
			else
			{
				text = text + "<a href='javascript:{GoPage(1);}'>" + Language.GetMemberMsg("PagedFirst", new string[0]) + "</a>   ";
				obj = text;
				text = string.Concat(new object[]
				{
					obj,
					"<a href='javascript:{GoPage(",
					this.P_indexPage - 1,
					");}'>",
					Language.GetMemberMsg("PagedOn", new string[0]),
					"</a>   "
				});
			}
			int num2;
			if (this.P_indexPage == num)
			{
				num2 = num;
			}
			else
			{
				num2 = this.P_indexPage + 1;
			}
			if (this.P_indexPage == num)
			{
				text = text + Language.GetMemberMsg("PagedDown", new string[0]) + "  ";
				text = text + Language.GetMemberMsg("PagedLast", new string[0]) + "  ";
			}
			else
			{
				obj = text;
				text = string.Concat(new object[]
				{
					obj,
					"<a href='javascript:{GoPage(",
					num2,
					");}'>    ",
					Language.GetMemberMsg("PagedDown", new string[0]),
					"</a>    "
				});
				text2 = text;
				text = string.Concat(new string[]
				{
					text2,
					"<a href='javascript:{GoPage(",
					num.ToString(),
					");}'>    ",
					Language.GetMemberMsg("PagedLast", new string[0]),
					"</a>     "
				});
			}
			obj = text;
			text = string.Concat(new object[]
			{
				obj,
				Language.GetMemberMsg("Paged2", new string[0]),
				"  ",
				this.P_indexPage,
				"/",
				num,
				" ",
				Language.GetMemberMsg("Paged", new string[0])
			});
			obj = text;
			text = string.Concat(new object[]
			{
				obj,
				" ",
				this.P_MaxPerPage,
				this.P_Object,
				"  ",
				Language.GetMemberMsg("Paged", new string[0])
			});
			if (this.P_isShowDownList)
			{
				text = text + Language.GetMemberMsg("to", new string[0]) + "<select name='page' onChange=\"javascript:{GoPage(this.options[this.selectedIndex].value);}\" >";
				for (int i = 1; i <= num; i++)
				{
					if (i >= 1000)
					{
						break;
					}
					obj = text;
					text = string.Concat(new object[]
					{
						obj,
						"<option value='",
						i,
						"'"
					});
					if (i == this.P_indexPage)
					{
						text += " selected ";
					}
					obj = text;
					text = string.Concat(new object[]
					{
						obj,
						">",
						Language.GetMemberMsg("Article", new string[0]),
						i,
						Language.GetMemberMsg("Paged", new string[0]),
						"</option>"
					});
				}
				text += "</select>";
			}
			return text;
		}
		public void Clear()
		{
			this.SQL_LeftJoinTable = null;
			this.SQL_LeftJoinField = null;
			this.SQL_JoinField = null;
			this.SQL_JoinField2 = null;
			this.P_indexPage = 1;
			this.P_MaxPerPage = 10;
			this.P_allNum = 0;
			this.P_Object = null;
			this.P_LinkHead = null;
		}
	}
}
